package com.yc.hwc.web;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.yc.hwc.bean.Result;
import com.yc.hwc.bean.User;
import com.yc.hwc.dao.IUserExDao;

@RestController
public class UserDo extends BaseAction<User> {

	@PostMapping("login")
	public Result<?> login(@RequestBody @Valid User user, Errors errors, HttpSession session) {
		if (errors.hasFieldErrors("account") || errors.hasFieldErrors("pwd")) {
			return Result.failure("登录信息填写不全", errors.getFieldErrors());
		}
		try {
			User dbUser = ((IUserExDao) biz.getDao()).findForLogin(user);
			if (dbUser != null) {
				session.setAttribute("loginedUser", dbUser);
				return Result.success("登录成功！", dbUser);
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return Result.failure("用户名或密码错误！请尝试使用姓名、邮箱、电话登录！");
	}

}
